package com.example.myblog_ssm.common;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-03-13
 * Time: 16:47
 */

import lombok.Data;

/**
 * 统一数据格式返回
 */
@Data
public class AjaxResult {
    
    //状态码
    private Integer code;
    //状态码描述信息
    private String msg;
    //返回的数据
    private Object data;

    /**
     * 超过成功返回的结果
     *
     * @param data
     * @return
     */
    public static AjaxResult success(Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(200);
        result.setMsg("");
        result.setData(data);
        return result;
    }

    public static AjaxResult success(int code, Object data) {
        AjaxResult result = new AjaxResult();
        result.setData(data);
        result.setCode(code);
        result.setMsg("");
        return result;
    }

    public static AjaxResult success(int code, String msg, Object data) {
        AjaxResult result = new AjaxResult();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 返回失败结果
     */
    public static AjaxResult fail(int code, String msg) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setMsg(msg);
        //返回失败,过为null
        result.setData(null);
        return result;
    }

    public static AjaxResult fail(int code, String msg, Object data) {
        AjaxResult result = new AjaxResult();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
